<?php

function initClassGrid()
{
    // Create heap
    createClassHeap();
    // Init grid
    $grid = new zeus_datagrid();
    $grid->oForm = new zeus_quickform(array("id" => "posts-filter", "method" => "get"));
    if (is_object($grid)) {
        // Set grid title
        $grid->sGridTitle = _e("Manage Classes");
        // Set id column
        $grid->sIdColumn = DB_PREFIX . "class_grid_view . myclass_id";
        // Set operate
        $grid->bEdit = can_operate('edit_class', $_SESSION['user_id']);
        $grid->bAdd = can_operate('add_class', $_SESSION['user_id']);
        $grid->bView = can_operate('view_class', $_SESSION['user_id']);
        $grid->bDelete = can_operate('del_class', $_SESSION['user_id']);
        $grid->bDebug = false;
        // Set submenu
        $grid->aSubMenu = null;
        // Set table
        $grid->sTable = array(DB_PREFIX . 'class_grid_view');
        // Set column display name
        $grid->aColumn = array("checkbox" => array("name" => $grid->oForm->addElement("checkbox", array("name" => "checkAllClass", "id" => "checkAllClass", "onclick" => "xajax_checkAllClass(this.checked);return false;"))),
            "class_name" => array("name" => _e("Class Name"), "field" => "myclass_name"),
            "master_id" => array("name" => _e("Class Master"), "field" => "master_id"),
            "description" => array("name" => _e("Description"), "field" => "description"),
            "comments" => array("name" => _e("Member Number"), "field" => "member_num"),
            "time" => array("name" => _e("Graduate Time"), "field" => "graduate_time"),
            "is_enable" => array("name" => _e("Status"), "field" => "is_enable"),
            );
        if (!$grid->bDelete) {
            $grid->aColumn = array_slice($grid->aColumn, 1);
        } else {
            zeus_do_nothing();
        }
        // Set fields
        $grid->aField = array(
            DB_PREFIX . "class_grid_view . myclass_id",
            DB_PREFIX . "class_grid_view . myclass_name",
            DB_PREFIX . "class_grid_view . description",
            DB_PREFIX . "class_grid_view . master_id",
            DB_PREFIX . "class_grid_view . is_enable",
            DB_PREFIX . "class_grid_view . graduate_time",
            DB_PREFIX . "class_grid_view . member_num"
            );
        // Set select
        $grid->aSelect = null;
        // Set match
        $grid->sMatch = null;
        // Set whole only
        $grid->bWholeOnly = false;
        // Set order
        $grid->sOrder = isset($_GET['order'])?$_GET['order']:null;
        // Sett order type
        $grid->sOrderType = isset($_GET['type'])?$_GET['type']:null;
        // Set status
        if (isset($_GET['status']) && null != $_GET['status']) {
            switch ($_GET['status']) {
                case "1":
                case "0":
                    $grid->bWholeOnly = true;
                    $grid->sFilter = $_GET['status'];
                    $grid->sFilterField = DB_PREFIX . "class_grid_view . is_enable";
                    break;
                default:
                    $grid->sFilterField = null;
                    $grid->sFilter = null;
                    break;
            }
        }
        // Set search
        elseif (isset($_GET['submit']) && (_e('Search')) == $_GET['submit']) {
            $grid->sFilter = $_GET['search'];
        }
        // Set filtrate field
        elseif (isset($_GET['filtrate']) && _e('Filtrate') == $_GET['filtrate']) {
            if (isset($_GET['filtrate_class']) && null != $_GET['filtrate_class']) {
                switch ($_GET['filtrate_class']) {
                    case "-1":
                        $grid->sFilterField = null;
                        $grid->sFilter = null;
                        break;
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                        $grid->sFilter = $_GET['filtrate_class'];
                        $grid->sFilterField = DB_PREFIX . "class_grid_view . myclass_id";
                        break;
                    default:
                        $grid->sFilterField = null;
                        $grid->sFilter = null;
                        break;
                }
            } else {
                $grid->sFilterField = null;
                $grid->sFilter = null;
            }
        } else {
            $grid->sFilterField = null;
            $grid->sFilter = null;
        }
    } else {
        zeus_do_nothing();
    }
    return $grid;
}

/**
 * getCurrentClass()
 *
 * @return
 */
function getCurrentClass()
{
    $sql = "SELECT myclass_id,myclass_name FROM " . DB_PREFIX . "myclass WHERE is_enable='1'";
    $result = zeus_sql_query($sql, false, 'All');
    if (null != $result) {
        foreach($result AS $value) {
            $id[] = $value['myclass_id'];
            $name[] = $value['myclass_name'];
        }
        $myclass = array_combine($id, $name);
        return $myclass;
    } else {
        return false;
    }
}
/**
 * getTheClassName()
 *
 * @param string $sClassId
 * @return
 */
function getTheClassName($sClassId = "")
{
    if ("" != $sClassId) {
        $sql = "SELECT myclass_name FROM " . DB_PREFIX . "myclass WHERE myclass_id='" . $sClassId . "'";
        $result = zeus_sql_query($sql, false);
        if (null != $result) {
            $myclass_name = $result['myclass_name'];
            return $myclass_name;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/**
 * showClassGridRow()
 *
 * @return
 */
function showClassGridRow($oGrid, $aData = array(), $sStyle = "")
{
    $row[] = "<tr id='class-" . $aData ['myclass_id'] . $sStyle . ">";
    if ($oGrid->bDelete) {
        $row[] = "<th scope='row' class='check-column'><input type='checkbox' name='delete[]' value='" . $aData ['myclass_id'] . "' /></th>" . $wrap;
    } else {
        $row[] = "";
    }
    if ($oGrid->bEdit) {
        $edit = "?action=edit_user&user_id=" . $aData ['myclass_id'];
    } elseif ($oGrid->bView) {
        $edit = "?action=view_user&user_id=" . $aData ['myclass_id'];
    } else {
        $edit = "#";
    }
    $row[] = "<td><strong><a href='" . $_SERVER['PHP_SELF'] . $edit . "'>" . $aData ['myclass_name'] . "</a></strong></td>";
    $row[] = "<td>" . $aData ['master_id'] . "</td>";
    $row[] = "<td>" . $aData ['description'] . "</td>";
    $row[] = "<td class='num'>" . $aData ['member_num'] . "</td>";
    $row[] = "<td>" . $aData ['graduate_time'] . "</td>";
    $row[] = "<td>" . $aData ['is_enable'] . "</td>";
    $row[] = "</tr>";
    return implode("\n\t", $row);
}
/**
 * getTermIndex()
 *
 * @return
 */
function getTermIndex()
{
    $currentYear = date("Y");
    $currentMonth = date("m");
    switch ($currentMonth) {
        case "7":
        case "8":
        case "9":
        case "10":
        case "11":
        case "12":
        case "1":
            $term = "A";
            break;
        case "2":
        case "3":
        case "4":
        case "5":
        case "6":
        case "1":
        case "1":
            $term = "B";
            break;
        default:
            $term = "A";
            break;
    }
    $index = $currentYear . $term;
    return $index;
}

/**
 * updateStudentClassId()
 *
 * @return
 */
function updateStudentClassId()
{
    $index = getTermIndex();
    $sql = "SELECT student_no,myclass_id FROM " . DB_PREFIX . "rel_student_info";
    $result = zeus_sql_query($sql, true, 'All');
    if (isset($result) && null != $result) {
        foreach($result AS $record) {
            $classId = substr($record['myclass_id'], 5, 2);
            $student_no[] = $record['student_no'];
            $id[$index] = $classId;
            $myclass_id[] = serialize($id);
        }
        foreach($result AS $key => $value) {
            $sql = "UPDATE " . DB_PREFIX . "rel_student_info SET myclass_id='" . $myclass_id[$key] . "' WHERE student_no='" . $student_no[$key] . "'";
            zeus_sql_exec($sql, true);
        }
    } else {
        return false;
    }
}



?>